package com.GlobalPaint.app.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.LinshiWirkBean;
import com.GlobalPaint.app.ui.HTML.YonggDetailsActivity;
import com.GlobalPaint.app.ui.Laborer.TemporaryWorkFragment;
import com.GlobalPaint.app.utils.Constants;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2018/1/7 0007.
 */

public class LinshiAdapter extends XRecyclerView.Adapter<LinshiAdapter.ViewHoler>{
    private List<LinshiWirkBean.DataBean> list;
    private TemporaryWorkFragment temporaryWorkFragment;
    private Context context;

    public LinshiAdapter(List<LinshiWirkBean.DataBean> list, TemporaryWorkFragment temporaryWorkFragment) {
        this.list=list;
        this.temporaryWorkFragment=temporaryWorkFragment;
    }

    @Override
    public LinshiAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.frament_recruit_linshi_item, parent, false);
        LinshiAdapter.ViewHoler holder = new LinshiAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final LinshiAdapter.ViewHoler holder, final int position) {
        String companyname = list.get(position).getContent() ==
                null ? "" : list.get(position).getContent();
        String title = list.get(position).getTitle() ==
                null ? "" : list.get(position).getTitle();
        String money = String.valueOf(list.get(position).getMoney()) ==
                null ? "" : String.valueOf(list.get(position).getMoney());
        String publishtime = list.get(position).getPublishtime() ==
                null ? "" : list.get(position).getPublishtime();
        String city = list.get(position).getLocation() ==
                null ? "" : list.get(position).getLocation();
        String img = list.get(position).getAvatar() ==
                null ? "wu" : list.get(position).getAvatar();
        holder.tv_companyname.setText(companyname);
        holder.tv_title.setText(title);
        holder.tv_money.setText(money);
        if(img.equals("wu")){
            Picasso.with(context).load(R.drawable.touxiang).into(holder.imge);
        }else {
            Picasso.with(context).load(Constants.JASON_SERVICE_URL_TOUXIANG+img)
                    .placeholder(R.drawable.touxiang).into(holder.imge);
        }
        if(publishtime.length()>10){
            holder.tv_publishtime.setText(publishtime.substring(0,10));
        }
        holder.tv_city.setText(city);
        if(list.get(position).getState()==1){
            holder.tvjiedan.setText("取消接单");
        }else if(list.get(position).getState()==2||list.get(position).getState()==0){
            holder.tvjiedan.setText("接单");
        }
        holder.mview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(view.getContext(), YonggDetailsActivity.class);
                intent.putExtra("fragment","linshi");
                intent.putExtra("money",list.get(position).getMoney());
                intent.putExtra("publish",list.get(position).getPublishtime());
                intent.putExtra("qq",list.get(position).getQq());
                intent.putExtra("telno",list.get(position).getTelno());
                intent.putExtra("title",list.get(position).getTitle());
                intent.putExtra("wx",list.get(position).getWx());
                intent.putExtra("memo",list.get(position).getMemo());
                intent.putExtra("content",list.get(position).getContent());
                intent.putExtra("begintime",list.get(position).getBegintime());
                intent.putExtra("endtime",list.get(position).getEndtime());
                intent.putExtra("hireInfoid",list.get(position).getId());
                intent.putExtra("Location",list.get(position).getLocation());
                intent.putExtra("state",list.get(position).getState());
                intent.putExtra("companyname",list.get(position).getCompanyname());
                intent.putExtra("creatId",list.get(position).getUserid());
                view.getContext().startActivity(intent);
            }
        });
        holder.tvjiedan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                int state=list.get(position).getState();
                int id=list.get(position).getId();
                if(state==0||state==2){
                    state=1;
                }else if(state==1){
                    state=2;
                }
                if(state==3){
                    Toast.makeText(view.getContext()," 不能自己接自己单",Toast.LENGTH_LONG).show();
                    return;
                }
                temporaryWorkFragment.jiedan(state,id);

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHoler extends RecyclerView.ViewHolder {

        private final ImageView imge;
        private final TextView tv_title;
        private final TextView tv_companyname;
        private final TextView tv_city;
        private final TextView tv_money;
        private final TextView tv_publishtime;
        private final TextView tvjiedan;
        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            imge = (ImageView)itemView. findViewById(R.id.imge);
            tv_title = (TextView)itemView.findViewById(R.id.tv_title);
            tv_companyname = (TextView)itemView.findViewById(R.id.tv_companyname);
            tv_city = (TextView)itemView.findViewById(R.id.tv_city);
            tv_money = (TextView)itemView.findViewById(R.id.tv_money);
            tv_publishtime = (TextView)itemView.findViewById(R.id.tv_publishtime);
            tvjiedan = (TextView)itemView.findViewById(R.id.tvjiedan);
        }
    }
}
